package com.lich;

public class CommonUtils {
    /**
     * 字符串判空
     *
     * @param cs
     * @return
     */
    public static boolean isEmptyString(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 下划线转换为驼峰
     *
     * @param underscoreName
     * @return
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (!isEmptyString(underscoreName)) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    /**
     * 将第一个字符变为大写
     *
     * @return
     */
    public static String upperFirstChar(String str) {
        if (!isEmptyString(str)) {
            str = str.substring(0, 1).toUpperCase() + str.substring(1);
            return str;
        } else {
            return str;
        }
    }

    /**
     * 移除前缀
     *
     * @return
     */
    public static String removePrefix(String str, String prefix) {
        if (!isEmptyString(str) && !isEmptyString(prefix)) {
            return str.startsWith(prefix) ? str.substring(prefix.length()) : str;
        } else {
            return str;
        }
    }

    /**
     * 移除前后缀
     *
     * @return
     */
    public static String removePrefixAndSuffix(String str, String prefix, String suffix) {
        if (isEmptyString(str) || isEmptyString(prefix) && isEmptyString(suffix)) {
            return str;
        }
        if (!isEmptyString(prefix)) {
            str = str.startsWith(prefix) ? str.substring(prefix.length()) : str;
        }
        if (!isEmptyString(suffix)) {
            str = str.endsWith(suffix) ? str.substring(0, str.length() - suffix.length()) : str;
        }
        return str;
    }
}
